package cn.edu.scau.cmi.hombio.service;

import cn.edu.scau.cmi.hombio.dao.PreventionTypeDAO;
import cn.edu.scau.cmi.hombio.dao.TaskDAO;
import cn.edu.scau.cmi.hombio.dao.TaskPreventionDAO;

import cn.edu.scau.cmi.hombio.domain.PreventionType;
import cn.edu.scau.cmi.hombio.domain.Task;
import cn.edu.scau.cmi.hombio.domain.TaskPrevention;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

/**
 * Spring service that handles CRUD requests for TaskPrevention entities
 * 
 */

@Service("TaskPreventionService")

@Transactional
public class TaskPreventionServiceImpl implements TaskPreventionService {

	/**
	 * DAO injected by Spring that manages PreventionType entities
	 * 
	 */
	@Autowired
	private PreventionTypeDAO preventionTypeDAO;

	/**
	 * DAO injected by Spring that manages Task entities
	 * 
	 */
	@Autowired
	private TaskDAO taskDAO;

	/**
	 * DAO injected by Spring that manages TaskPrevention entities
	 * 
	 */
	@Autowired
	private TaskPreventionDAO taskPreventionDAO;

	/**
	 * Instantiates a new TaskPreventionServiceImpl.
	 *
	 */
	public TaskPreventionServiceImpl() {
	}

	/**
	 * Return a count of all TaskPrevention entity
	 * 
	 */
	@Transactional
	public Integer countTaskPreventions() {
		return ((Long) taskPreventionDAO.createQuerySingleResult("select count(o) from TaskPrevention o").getSingleResult()).intValue();
	}

	/**
	 * Delete an existing Task entity
	 * 
	 */
	@Transactional
	public TaskPrevention deleteTaskPreventionTask(Integer taskprevention_id, Integer related_task_id) {
		TaskPrevention taskprevention = taskPreventionDAO.findTaskPreventionByPrimaryKey(taskprevention_id, -1, -1);
		Task related_task = taskDAO.findTaskByPrimaryKey(related_task_id, -1, -1);

		taskprevention.setTask(null);
		related_task.getTaskPreventions().remove(taskprevention);
		taskprevention = taskPreventionDAO.store(taskprevention);
		taskPreventionDAO.flush();

		related_task = taskDAO.store(related_task);
		taskDAO.flush();

		taskDAO.remove(related_task);
		taskDAO.flush();

		return taskprevention;
	}

	/**
	 * Delete an existing TaskPrevention entity
	 * 
	 */
	@Transactional
	public void deleteTaskPrevention(TaskPrevention taskprevention) {
		taskPreventionDAO.remove(taskprevention);
		taskPreventionDAO.flush();
	}

	/**
	 * Return all TaskPrevention entity
	 * 
	 */
	@Transactional
	public List<TaskPrevention> findAllTaskPreventions(Integer startResult, Integer maxRows) {
		return new java.util.ArrayList<TaskPrevention>(taskPreventionDAO.findAllTaskPreventions(startResult, maxRows));
	}

	/**
	 * Save an existing PreventionType entity
	 * 
	 */
	@Transactional
	public TaskPrevention saveTaskPreventionPreventionType(Integer id, PreventionType related_preventiontype) {
		TaskPrevention taskprevention = taskPreventionDAO.findTaskPreventionByPrimaryKey(id, -1, -1);
		PreventionType existingpreventionType = preventionTypeDAO.findPreventionTypeByPrimaryKey(related_preventiontype.getId());

		// copy into the existing record to preserve existing relationships
		if (existingpreventionType != null) {
			existingpreventionType.setId(related_preventiontype.getId());
			existingpreventionType.setName(related_preventiontype.getName());
			existingpreventionType.setReportQuality(related_preventiontype.getReportQuality());
			existingpreventionType.setQuality(related_preventiontype.getQuality());
			existingpreventionType.setMemo(related_preventiontype.getMemo());
			related_preventiontype = existingpreventionType;
		}

		taskprevention.setPreventionType(related_preventiontype);
		related_preventiontype.getTaskPreventions().add(taskprevention);
		taskprevention = taskPreventionDAO.store(taskprevention);
		taskPreventionDAO.flush();

		related_preventiontype = preventionTypeDAO.store(related_preventiontype);
		preventionTypeDAO.flush();

		return taskprevention;
	}

	/**
	 * Save an existing TaskPrevention entity
	 * 
	 */
	@Transactional
	public void saveTaskPrevention(TaskPrevention taskprevention) {
		TaskPrevention existingTaskPrevention = taskPreventionDAO.findTaskPreventionByPrimaryKey(taskprevention.getId());

		if (existingTaskPrevention != null) {
			if (existingTaskPrevention != taskprevention) {
				existingTaskPrevention.setId(taskprevention.getId());
				existingTaskPrevention.setMemo(taskprevention.getMemo());
			}
			taskprevention = taskPreventionDAO.store(existingTaskPrevention);
		} else {
			taskprevention = taskPreventionDAO.store(taskprevention);
		}
		taskPreventionDAO.flush();
	}

	/**
	 * Load an existing TaskPrevention entity
	 * 
	 */
	@Transactional
	public Set<TaskPrevention> loadTaskPreventions() {
		return taskPreventionDAO.findAllTaskPreventions();
	}

	/**
	 */
	@Transactional
	public TaskPrevention findTaskPreventionByPrimaryKey(Integer id) {
		return taskPreventionDAO.findTaskPreventionByPrimaryKey(id);
	}

	/**
	 * Save an existing Task entity
	 * 
	 */
	@Transactional
	public TaskPrevention saveTaskPreventionTask(Integer id, Task related_task) {
		TaskPrevention taskprevention = taskPreventionDAO.findTaskPreventionByPrimaryKey(id, -1, -1);
		Task existingtask = taskDAO.findTaskByPrimaryKey(related_task.getId());

		// copy into the existing record to preserve existing relationships
		if (existingtask != null) {
			existingtask.setId(related_task.getId());
			existingtask.setTaskNumber(related_task.getTaskNumber());
			existingtask.setCreateTime(related_task.getCreateTime());
			existingtask.setStartTime(related_task.getStartTime());
			existingtask.setEndTime(related_task.getEndTime());
			existingtask.setMemo(related_task.getMemo());
			related_task = existingtask;
		}

		taskprevention.setTask(related_task);
		related_task.getTaskPreventions().add(taskprevention);
		taskprevention = taskPreventionDAO.store(taskprevention);
		taskPreventionDAO.flush();

		related_task = taskDAO.store(related_task);
		taskDAO.flush();

		return taskprevention;
	}

	/**
	 * Delete an existing PreventionType entity
	 * 
	 */
	@Transactional
	public TaskPrevention deleteTaskPreventionPreventionType(Integer taskprevention_id, Integer related_preventiontype_id) {
		TaskPrevention taskprevention = taskPreventionDAO.findTaskPreventionByPrimaryKey(taskprevention_id, -1, -1);
		PreventionType related_preventiontype = preventionTypeDAO.findPreventionTypeByPrimaryKey(related_preventiontype_id, -1, -1);

		taskprevention.setPreventionType(null);
		related_preventiontype.getTaskPreventions().remove(taskprevention);
		taskprevention = taskPreventionDAO.store(taskprevention);
		taskPreventionDAO.flush();

		related_preventiontype = preventionTypeDAO.store(related_preventiontype);
		preventionTypeDAO.flush();

		preventionTypeDAO.remove(related_preventiontype);
		preventionTypeDAO.flush();

		return taskprevention;
	}
}
